package com.san.oms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.san.oms.dao.ScoreLevelDao;
import com.san.oms.dao.ScoreLevelDetailDao;
import com.san.oms.domain.ScoreLevelDO;
import com.san.oms.domain.ScoreLevelDetailDO;
import com.san.oms.service.ScoreLevelService;
import com.san.oms.vo.ScoreLevelVO;

/**
 * 评分等级实现
 * @author SuperSR
 * @date 2019年2月27日 上午9:35:21
 *
 */
@Service
public class ScoreLevelServiceImpl implements ScoreLevelService{

	@Autowired
	private ScoreLevelDao dao;
	@Autowired
	private ScoreLevelDetailDao detaildao;
	
	@Override
	public ScoreLevelDO getById(Long id) {
		return dao.getById(id);
	}

	@Override
	public List<ScoreLevelDO> list(Map<String, Object> map) {
		return dao.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		return dao.count(map);
	}
	
	@Override
	public int save(ScoreLevelVO scoreLevelVO) {
		ScoreLevelDO scoreLevelDO = getByName(scoreLevelVO.getScoreName());
		if(scoreLevelDO != null){
			return -1;
		}else{
			return dao.save(scoreLevelVO);
		}
	}

	@Override
	public int doEdit(ScoreLevelVO scoreLevelVO) {
		ScoreLevelDO scoreLevelDO = getByName(scoreLevelVO.getScoreName());
		if(scoreLevelDO != null && !scoreLevelDO.getScoreName().equals(scoreLevelVO.getOldName())){
			return -1;
		}else{
			return dao.doEdit(scoreLevelVO);
		}
	}

	@Override
	public int remove(Long id) {
		Map<String, Object> map = new HashMap<>();
		map.put("scoreId", id);
		List<ScoreLevelDetailDO> list =  detaildao.list(map);
		if(list != null && list.size() > 0){//若该评分等级存在明细，则返回-1
			return -1;
		}else{
			return dao.remove(id);
		}
	}

	@Override
	public int batchRemove(Long[] ids) {
		for (Long id : ids) {
			Map<String, Object> map = new HashMap<>();
			map.put("scoreId", id);
			List<ScoreLevelDetailDO> list =  detaildao.list(map);
			if(list != null && list.size() > 0){//若有评分等级存在明细，则返回-1
				return -1;
			}
		}
		return dao.batchRemove(ids);
	}

	@Override
	public ScoreLevelDO getByName(String name) {
		return dao.getByName(name);
	}

	@Override
	public List<ScoreLevelDO> getScoreName() {
		return dao.getScoreName();
	}

}
